package com.njitzx.fileupload.service.impl;

import com.njitzx.fileupload.mapper.CommentMapper;
import com.njitzx.fileupload.pojo.Comment;
import com.njitzx.fileupload.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.njitzx.fileupload.constant.RedisConstant.articleComment;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void addComment(Comment comment) {
//        UserDTO user = UserHolder.getUser();
//        Integer  userId = user.getId();
        comment.setUserId(2);
        comment.setCreatedDate(new Date());
        comment.setUpdatedDate(new Date());
        comment.setIsDeleted(0);
        commentMapper.addComment(comment);
        redisTemplate.delete(articleComment + comment.getArticleId());  // 删除该文章的评论缓存
    }

    public List<CommentVO> getCommentById(Integer id) {
        // 从redis中获取评论列表
        List<CommentVO> commentVOList = (List<CommentVO>) redisTemplate.opsForValue().get(articleComment + id);

        if (commentVOList != null) {
            return commentVOList;
        }
        // 传过来文章的id，查出该文章的所有评论
        List<Comment> comments = commentMapper.getCommentById(id);

        // 找到所有一级评论并转化为 CommentVO
        commentVOList = comments.stream()
                .filter(comment -> comment.getParentId() == null)  // 一级评论
                .map(comment -> {
                    // 创建 CommentVO 并填充数据
                    CommentVO commentVO = new CommentVO();
                    commentVO.setCommentId(comment.getCommentId());
                    commentVO.setArticleId(comment.getArticleId());
                    commentVO.setUserId(comment.getUserId());
                    commentVO.setContent(comment.getContent());
                    commentVO.setParentId(comment.getParentId());
                    commentVO.setCreatedDate(comment.getCreatedDate());
                    commentVO.setUpdatedDate(comment.getUpdatedDate());
                    commentVO.setIsDeleted(comment.getIsDeleted());

                    // 查找所有子评论（二级评论），这些评论的 parentId 为当前一级评论的 commentId
                    List<CommentVO> childrenVO = comments.stream()
                            .filter(child -> comment.getCommentId().equals(child.getParentId()))  // 二级评论，parentId 为当前一级评论的 commentId
                            .map(childComment -> {
                                CommentVO childCommentVO = new CommentVO();
                                childCommentVO.setCommentId(childComment.getCommentId());
                                childCommentVO.setArticleId(childComment.getArticleId());
                                childCommentVO.setUserId(childComment.getUserId());
                                childCommentVO.setContent(childComment.getContent());
                                childCommentVO.setParentId(childComment.getParentId());
                                childCommentVO.setCreatedDate(childComment.getCreatedDate());
                                childCommentVO.setUpdatedDate(childComment.getUpdatedDate());
                                childCommentVO.setIsDeleted(childComment.getIsDeleted());
                                return childCommentVO;
                            })
                            .collect(Collectors.toList());

                    // 将子评论设置到当前一级评论的 children 字段中
                    commentVO.setChildren(childrenVO);
                    return commentVO;
                })
                .collect(Collectors.toList());
        redisTemplate.opsForValue().set(articleComment + id, commentVOList);
        return commentVOList;  // 返回包含一级评论及其子评论的列表
    }

}
